package com.wuming.caiwu.service.impl;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.MenuMapper;
import com.wuming.caiwu.model.Menu;
import com.wuming.caiwu.service.MenuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl implements MenuService {

    @Autowired
    private MenuMapper cmsBannerMapper;


    @Override
    public void saveMenu(Menu student) throws BusinessException {
        cmsBannerMapper.insert(student);
    }

    @Override
    public void modifyMenu(Menu student) throws BusinessException {
        cmsBannerMapper.updateByPrimaryKey(student);
    }

    @Override
    public List<Menu> getMenusByPage(String menuName, int pageIndex, int pageSize) throws BusinessException {
        try {
            Map<Object, Object> maps = new HashMap<Object, Object>();
            if (StringUtils.isNotBlank(menuName)) {
                maps.put("menuName", menuName);
            }
//            if (null != endTime) {
//                maps.put("endTime", endTime);
//            }
            return (List<Menu>) this.getPageList(MenuMapper.class, "selectByParams", maps, pageIndex, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String menuName) throws BusinessException {
        try {
            Map<Object, Object> maps = new HashMap<Object, Object>();
            if (StringUtils.isNotBlank(menuName)) {
                maps.put("menuName", menuName);
            }
//            if (null != endTime) {
//                maps.put("endTime", endTime);
//            }
            return this.getTotal(MenuMapper.class, "selectTotal", maps);
        } catch (Exception e) {
            logger.error("查询banner信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public void removeMenuById(Long id) throws BusinessException {
        cmsBannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Menu getMenuById(Long id) throws BusinessException {
        return cmsBannerMapper.selectByPrimaryKey(id);
    }
}
